<?php
namespace app\api\controller;

use app\api\model\Goods;
use think\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Request;
use app\api\model\Warehouse as WarehouseModel;
use app\api\validate\WarehouseValidate;
use think\Db;
use app\http\TokenTrait;
class Warehouse extends Common
{
    use TokenTrait;
    /**
     * 仓库列表查询  模糊查询
     * @param Request $request
     * @param WarehouseModel $warehouse
     * @return Json
     * @throws \think\exception\DbException
     */
    public function index(Request $request)
    {
        $warehouse = new WarehouseModel();
        $list = $warehouse->getList(
            $request->param('name'),
            $request->param('status'),
            $request->param('page'),
            $request->param('size')
        );
        if (!empty($list)) {
            return $this->returnJson(200, '成功', $list);
        } else {
            return $this->returnJson(404, '失败');
        }
    }

    public function getAllWarehouse(WarehouseModel $warehouse)
    {
        return $this->returnJson(200,'成功',$warehouse->getAll());
    }

    /**
     * @param Request $request
     * @param WarehouseModel $model
     * @return array|\PDOStatement|string|\think\Model
     */
    public function show(Request $request, WarehouseModel $warehouse)
    {
        try {
            return $this->returnJson(200, '成功', $warehouse->getInfo($request->param('id', '')));
        } catch (DataNotFoundException $e) {
            return $this->returnJson(404, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(404, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * 仓库新增
     * @return Json
     */
    public function add(Request $request, WarehouseModel $warehouse)
    {
        $data = $request->param();
        $data['create_time'] = time();
        $data['status'] =isset($data['status']) ? $data['status'] : 2;
        $validate = new WarehouseValidate();
        if (!$validate->check($data)) {
            return json(['code' => 404, 'msg' => $validate->getError()]);
        }
        $list = $warehouse->getWarehouseAdd($data);
        if (!empty($list)) {
            return $this->returnJson(200, '成功', $list);
        } else {
            return $this->returnJson(404, '失败');
        }
    }

    public function getPerson(Request $request, WarehouseModel $warehouse)
    {
        return $warehouse->getPerson();
    }

    /**
     * 仓库修改
     * @return Json
     */
    public function edit($id, Request $request, WarehouseModel $warehouse)
    {
        $data = $request->param();
        $data['edit_time'] = time();
        $data['status'] =isset($data['status']) ? $data['status'] : 2;
        $validate = new WarehouseValidate();
        if (!$validate->check($data)) {
            return json(['code' => 404, 'msg' => $validate->getError()]);
        }
        $list = $warehouse->getWarehouseEdit($id, $data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 仓库删除
     * @param int $id 仓库id
     * @param WarehouseModel $warehouse
     * @return Json
     */
    public function del($id, WarehouseModel $warehouse)
    {
        try {
            $list = $warehouse->getWarehouseDel($id);
            return $this->returnJson(200, '成功', $list);
        } catch (PDOException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (Exception $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * 生产入库 列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ProductionList(Request $request, WarehouseModel $warehouse)
    {
        $data = $request->param();
        $data['start_time'] = isset($data['start_time']) ? strtotime($data['start_time']) : "";
        $data['end_time'] = isset($data['end_time']) ? strtotime($data['end_time']) : "";
        $data['number'] = isset($data['number']) ? $data['number'] : "";
        $list = $warehouse->getProductionList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    public function productionAdd(Request $request, WarehouseModel $warehouse)
    {
        $data = $request->param();
        $data['create_time'] = isset($data['create_time']) ? strtotime($data['create_time']) : "";
        $data['number'] = 'jxc' . time();
        $data['type'] =2;
        $data['maker'] = self::getUId();//制单人id
        $list = $warehouse->getproductionAdd($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    public function productionDetail($id, WarehouseModel $warehouse)
    {
        $list = $warehouse->getProductionDetail($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    public function productionRevoke($id, WarehouseModel $warehouse)
    {
        try {
            $list = $warehouse->getProductionRevoke($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    public function getPutProduct($id,Request $request,WarehouseModel $warehouse)
    {
        try{
            $data =$request->post();
            $data['uid'] = self::getUId();//制单人id
            $data['create_time'] =isset($data['create_time']) ? strtotime($data['create_time']) : "";
            $data['type'] =2;//2 生产入库单
            $data['status'] =4;
            $arr = $warehouse->getPutStock($id,$data);
            if(is_int($arr)){
                return $this->returnJson(200,  'ok');
            }else{
                return $this->returnJson(500,  $arr);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }

    public function productionDraft($id,Request $request,WarehouseModel $warehouse)
    {
        $data =$request->post();
        $data['maker'] = self::getUId();//制单人id
        $data['create_time'] =isset($data['create_time']) ? strtotime($data['create_time']) : "";
        $data['type'] =2;//2 生产入库单
        $data['status'] =1;
        $list = $warehouse->getProductionDraft($id,$data);
        if (is_int($list)) {
            return $this->returnJson(200,'ok',$list);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function productionDepot($id, Request $request, WarehouseModel $warehouse)
    {
        $list = $warehouse->getProductionDepot($id);
        if ($list ==1) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '输入数量超出库存上限']);
        }
    }

    public function productionDel($id, WarehouseModel $warehouse)
    {
        try {
            $list = $warehouse->getProductionDel($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    /**
     * 调拨 列表
     * @return \think\response\Json
     */
    public function AllotList(Request $request, WarehouseModel $warehouse)
    {
        $data = $request->param();
        $data['start_time'] = isset($data['start_time']) ? strtotime($data['start_time']) : "";
        $data['end_time'] = isset($data['end_time']) ? strtotime($data['end_time']) : "";
        $data['number'] = isset($data['number']) ? $data['number'] : "";
        $list = $warehouse->getAllotList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * 调拨 新增
     * @return \think\response\Json
     */
    public function AllotAdd(Request $request, WarehouseModel $warehouse)
    {
        $data = $request->param();
        $data['number'] = 'jxc'.time();
        $data['maker'] = self::getUId();//制单人id
        $data['type'] = 3;
        $data['create_time'] = isset($data['create_time']) ? strtotime($data['create_time']) : '';
        $list = $warehouse->getAllotAdd($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * 调拨单  ------详情
     * @param $id 调拨单id
     * @return \think\response\Json
     */
    public function AllotDetail($id, WarehouseModel $warehouse)
    {
        $list = $warehouse->getAllotInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 调拨单  ------调拨
     * @param $id 调拨单id
     * @return \think\response\Json
     */
    public function allot($id,Request $request, WarehouseModel $warehouse)
    {
        $data =$request->post();
        $data['type'] = 3;
        $data['status'] = 2;
        $data['create_time'] = isset($data['create_time']) ? strtotime($data['create_time']) : '';
        $list = $warehouse->getAllot($id,$data);
        if (is_int($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    public function AllotDraft($id,Request $request,WarehouseModel $warehouse)
    {
        $data =$request->post();
        $data['type'] = 3;
        $data['create_time'] = isset($data['create_time']) ? strtotime($data['create_time']) : '';
        $data['status'] = 1;
        $list = $warehouse->getAllotDraft($id,$data);
        if (is_int($list)) {
            return $this->returnJson(200,'ok',$list);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    /**
     * 调拨单  ------撤销
     * @param $id 调拨单id
     * @return \think\response\Json
     */
    public function AllotRevoke($id, WarehouseModel $warehouse)
    {
        try {
            $list = $warehouse->getAllotRevoke($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    /**
     * 调拨单  ------删除
     * @param $id 调拨单id
     * @return \think\response\Json
     */
    public function AllotDel($id, WarehouseModel $warehouse)
    {
        try {
            $list = $warehouse->getAllotDel($id);
            if (is_string($list)) {
                return $this->returnJson(500,$list);
            } else {
                return $this->returnJson(200,'ok');
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    public function InventoryList(Request $request, WarehouseModel $warehouse)
    {
        $data = $request->param();
        $data['start_time'] = isset($data['start_time']) ? strtotime($data['start_time']) : "";
        $data['end_time'] = isset($data['end_time']) ? strtotime($data['end_time']) : "";
        $data['number'] = isset($data['number']) ? $data['number'] : "";
        $list = $warehouse->getInventoryList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function InventoryDetailed(Request $request, WarehouseModel $warehouse)
    {
        $data = $request->param();
        $list = $warehouse->getInventoryDetailed($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function InventoryAdd(Request $request, WarehouseModel $warehouse)
    {
        $data = $request->param();
        $data['number'] = 'jxc' . time();
        $data['create_time'] = time();
        $list = $warehouse->getInventoryAdd($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function InventoryDetail($id, WarehouseModel $warehouse)
    {
        $list = $warehouse->getInventoryInfo($id);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    public function InventoryDel($id, WarehouseModel $warehouse)
    {
        try {
            $list = $warehouse->getInventoryDel($id);
            if (is_int($list)) {
                return $this->returnJson(200,'ok');
            } else {
                return $this->returnJson(500,$list);
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    public function InventoryRevoke($id, WarehouseModel $warehouse)
    {
        try {
            $list = $warehouse->getInventoryRevoke($id);
            if (is_int($list)) {
                return $this->returnJson(200,'ok');
            } else {
                return $this->returnJson(500,$list);
            }
        } catch (\Exception $e) {
            return $this->returnJson(500,$e->getMessage());
        }
    }

    public function getInventoryDraft($id,Request $request,WarehouseModel $warehouse)
    {
        try{
            $data =$request->post();
            $data['create_time'] =time();
            $data['status'] =3;
            $list = $warehouse->getInventoryDraft($id,$data);
            if (!empty($list)) {
                return $this->returnJson(200,'ok',$list);
            } else {
                return json(['code' => 404, 'msg' => '失败']);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }

    public function getInventory($id,Request $request,WarehouseModel $warehouse)
    {
        try{
            $data =$request->post();
            $data['create_time'] =time();
            $data['status'] =1;
            $arr = $warehouse->getInventory($id,$data);
            if(is_int($arr)){
                return $this->returnJson(200,  'ok');
            }else{
                return $this->returnJson(500,  $arr);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }
}